package com.contory.ssn_lgnd.app.maingame;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

import android.opengl.GLES20;
import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.ESShaderHandleNames;
import com.contory.ssn_lgnd.app.GameScreenLoader;
import com.contory.ssn_lgnd.app.KeyFrameAnimation;
import com.contory.ssn_lgnd.app.comp.ButtonObject;
import com.contory.ssn_lgnd.app.comp.ButtonObjectPtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.app.maingame.event.EnemyCreateEvent;
import com.contory.ssn_lgnd.app.maingame.event.EnemyGoEvent;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.gl.Animation;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.impl.GLGame;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class MainGameSceneStage1 extends MainGameSceneObjectPtr{
	public MainGameSceneStage1(MainGame game){
		super(game);
	}

	@Override
	public void update(float deltaTime) {
		switch(state){
			case 0:
				if(g.player.bodyView.y < 5){
					g.player.bodyView.y = 5;
					state = 1;
					g.player.nDir.set(0, 0);
					g.player.velocity.set(0, 0);
					g.player.setState(Player.PLAYALBE);
				}
				defaultUpdate(deltaTime);
			break;
			
			case 1:
				g.controler.update(deltaTime);
				if(g.controler.pauseBool)
					state = 2;
				defaultUpdate(deltaTime);
			break;
			
			case 2:
				g.controler.update(deltaTime);
				if(!g.controler.pauseBool)
					state = 1;
			break;
			
			case 3:
				GameScreenLoader.gameLoad(g.getGLGame(), GameScreenLoader.SCREEN_TITLE);
			break;
		}
	}
	
	private void defaultUpdate(float deltaTime){
		g.player.update(deltaTime);
		g.infoGui.update(deltaTime);
		
		g.curtain.update(deltaTime);
		g.enemyMG.update(deltaTime);

		g.fire.update(deltaTime);
		
		g.pEftGroup.update(deltaTime);
		g.tEftGroup.update(deltaTime);
		g.ptc.update(deltaTime);
		g.notifyMG.update(deltaTime);
	}
	
	@Override
	public void draw(SpriteBatcher batcher) {
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
		
		g.defaultShader.bind();
		g.cam.setViewPort(GLGame.frameWidth, GLGame.frameHeight);
		g.cam.setMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
		g.notifyMG.draw(batcher);
		g.pEftGroup.draw(batcher);
		g.tEftGroup.draw(batcher);
		g.fire.draw(batcher);
		g.enemyMG.draw(batcher);
		g.teraGroup.draw(batcher);
  		g.player.draw(batcher);
	  	batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
	  	
  	  	g.circleShader.bind();
		g.cam.setMatrices(g.circleShader, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(g.circleShader, Assets.tex2, ESShaderHandleNames.Uni_Sample0, 0);
		g.player.rangeViewDraw(batcher);
		batcher.endBatch(g.circleShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
	  	
	  	g.defaultShader.bind();
		g.cam.setOrinMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
		g.controler.draw(batcher);
		g.infoGui.draw(batcher);
		batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
		
		switch(state){
			case 0:
				g.defaultShader.bind();
				g.cam.setOrinMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
				batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
				Assets.font.drawText(batcher, "R E A D Y",
						g.cam.getFrustumWidth() / 2, g.cam.getFrustumHeight() / 2, 1, 1, 1, 1, 1, 1);
				batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
			break;
			case 2:
				g.defaultShader.bind();
				g.cam.setOrinMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
				batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
				batcher.drawSprite(g.cam.getFrustumWidth() / 2, g.cam.getFrustumHeight() / 2, 
						g.cam.getFrustumWidth(), g.cam.getFrustumHeight(), 
						0.5f, 0.5f, 0.5f, 0.5f, Assets.tex_reg_whiteTexel);
				Assets.font.drawText(batcher, "P A U S E",
						g.cam.getFrustumWidth() / 2, g.cam.getFrustumHeight() / 2, 1, 1, 1, 1, 1, 1);
				batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
			break;
			case 3:
				g.defaultShader.bind();
				g.cam.setOrinMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
				batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
				batcher.drawSprite(g.cam.getFrustumWidth() / 2, g.cam.getFrustumHeight() / 2, 
						g.cam.getFrustumWidth(), g.cam.getFrustumHeight(), 
						0.5f, 0.5f, 0.5f, 0.5f, Assets.tex_reg_whiteTexel);
				Assets.font.drawText(batcher, "D E M O   E N D",
						g.cam.getFrustumWidth() / 2, g.cam.getFrustumHeight() / 2, 1, 1, 1, 1, 1, 1);
				batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
			break;
		}
		
		g.defaultShader.bind();
		g.cam.setOrinMatrices(g.defaultShader, ESShaderHandleNames.Uni_MvpMatrix);
		batcher.beginBatch(g.defaultShader, Assets.tex1, ESShaderHandleNames.Uni_Sample0, 0);
		g.controler.drawOpBtr(batcher);
		batcher.endBatch(g.defaultShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
	  	
		if(g.curtain.getEnableBool()){
			g.curtainShader.bind();
			GLES20.glUniform1f(g.curtainShader.getUniform(ESShaderHandleNames.Uni_InnerVignetting), 1.0f - g.curtain.getVignettingValue());
	        GLES20.glUniform1f(g.curtainShader.getUniform(ESShaderHandleNames.Uni_OuterVignetting), 1.4f - g.curtain.getVignettingValue());
	        g.cam.setMatrices(g.curtainShader, ESShaderHandleNames.Uni_MvpMatrix);
			batcher.beginBatch(g.curtainShader, Assets.tex2, ESShaderHandleNames.Uni_Sample0, 0);
			g.curtain.draw(batcher);
			batcher.endBatch(g.curtainShader, ESShaderHandleNames.Attr_Pos, ESShaderHandleNames.Attr_Color, ESShaderHandleNames.Attr_TexCoord, ESShaderHandleNames.Uni_HasColor);
		}
	}
	
	@Override
	public void setTouchEvent(TouchEvent touchEvent, float touchX, float touchY) {
		if(state != 0 && state != 3){
			g.controler.setTouchEvent(touchEvent, touchX, touchY);
		}
	}
	
	//Share instance initalize section
	@Override
	public void initialize(){
		state = 0;
		g.cam.setPosit(g.cam.getFrustumWidth() / 2, 
				g.cam.getFrustumHeight() / 2);
		g.curtain.setOpenCurtain();
		
		g.ptc.setState(PlayerTraceCam.STATE_RIGHT_GO);
		
		g.player.setPos(1, 11);
		g.player.setState(Player.NONEPLAYABLE);
		g.player.nDir.set(0, -1);
		g.player.velocity.set(0, -Player.MAX_VELOCITY_SCALA);
		
		g.notifyMG.addNotifier(5, 5, 0.1f, 10);
		g.notifyMG.addEventGroup();
		EnemyGroup enemyGroup = g.enemyMG.addEnemyGroup();
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 10, 5));
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 13, 5));
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 12, 5));
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 15, 5));
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 14, 5));
		g.notifyMG.addEvent(new EnemyCreateEvent(g.enemyMG, enemyGroup, 11, 5));
	}
}
